package com.huiminpay.transaction.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Controller
public class PayTestController {
//沙箱环境应用的id
    String APP_ID = "2021000117679852";
//    私钥
    String APP_PRIVATE_KEY ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDRgKNIwtJm1MrUhkAFPta3RaxTjNPDesIq5jJgoOh8dtTmVm6G9GsRZ05Btz0+lzt/+zxq2eh+uuXc92BA72V/eoNhKVRNTh1w+1y10+fA5ljdULcYIop4svN8EyHf4+nrMoJgSX1yepLNl/ltFe3CpBNdxFBwv+boh1spvwWigDFY5QxX7xxKaE9XDJXhUrR+OBsiCBI/BdPdF0ZGL2bTXrAGQ1O1RCRutTP91BbR8QaxzUfbH8PWzbMoqVugx3jvN0mo754VKAPJKrDD1I1TSUayn21gbE83Djb90/nLGnGjawrex5U4fHnIdxsEvdWPtjxwi6uQEGVFCBJmI/GxAgMBAAECggEADaJKfgy3tpt4i7Yk6nJ5dLnjChjrGxidFD+g3ar7FkaBem/7h2rWoO5RKHOtV6pt49lMyENgHwc4uFWc/PzXZSFq9WLEKre/sL+a+/ZLacFT3StIjxiIXGyA7f0swi5J5dWGqI9/y3hwn1KZl+DKdVVKMVr6RSwn8dAFPcVBr5578WYDMKt2iXwnZvgtOMh6NQ+qidC/TtwZN0+cKu+IJUo4B9R1GhVr0sM8IkV/5wHL50svqr2Xld9O3Qu+29hfFBgg1A65t6g8W6Epq5Vp4Lu8dH7S7/vV7yF4ERfwiLawKNSrB6FMvbOloZohiFqTcGOz958MQ35Gtg8Tu4m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+p0byefba2Vt4XKDDWo8Jn3GgDOQJwhOmNr6PlNYcI9GaQFUyUSI/IBi78csq15bh5R7I8woxFcgj1bleC0HFTBehIXrLeU/OaMC6XO/Y1pH5n20RB6Id+sG6s4iVOTeyy9enXDGojIZ26HNuxhmVMDT8FddPss7frP2IRPHqnJygCo/LUMtOH1q9fc0mnwWdoHc84Jc+qE2plMHRT2EpR95PgnWUoCv/gAQIDAQAB";
    String CHARSET = "utf8";
//    沙箱环境网管地址
    String serverUrl = "https://openapi.alipaydev.com/gateway.do";//正式"https://openapi.alipay.com/gateway.do"
//    签名算法类型
    String sign_type = "RSA2";

    @GetMapping("test1")
    @ResponseBody
    public String test(){
        return "test1";
    }

    @GetMapping("aliPayTest")
    public void aliPayTest(HttpServletRequest httpRequest,
                       HttpServletResponse httpResponse) throws ServletException, IOException {
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"); //获得初始化的AlipayClient
        AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();//创建API对应的request
//        alipayRequest.setReturnUrl("http://domain.com/CallBack/return_url.jsp");
//        alipayRequest.setNotifyUrl("http://domain.com/CallBack/notify_url.jsp");//在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent("{" +
                " \"out_trade_no\":\"20150320010101002\"," +//订单测试需要手动改
                " \"total_amount\":\"88.88\"," +
                " \"subject\":\"Iphone6 16G\"," +
                " \"product_code\":\"QUICK_WAP_PAY\"" +
                " }");//填充业务参数
        String form="";
        try {
            form = alipayClient.pageExecute(alipayRequest).getBody(); //调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);//直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
}
